package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements IRoleService {

	@Autowired
	private RoleMapper roleMapper;

	public void delete(Long id) {
		roleMapper.delete(id);
		roleMapper.deletePermissionRelation(id);
		roleMapper.deleteEmployeeRelation(id);
	}

	public void insert(Role role, Long[] ids) {
		roleMapper.insert(role);
		if(ids != null){
			for (Long permissionId : ids) {
				roleMapper.insertRelation(role.getId(),permissionId);
			}
		}
	}

	public Role get(Long id) {
		return roleMapper.get(id);
	}

	public List<Role> selectAll() {
		return roleMapper.selectAll();
	}

	public void update(Role role, Long[] ids) {
		roleMapper.deletePermissionRelation(role.getId());
		if(ids != null){
			for (Long permissionId : ids) {
				roleMapper.insertRelation(role.getId(),permissionId);
			}
		}
		roleMapper.update(role);
	}

	public PageInfo query(QueryObject qo) {
		PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
		return new PageInfo(roleMapper.selectForList(qo));
	}

	@Override
	public List<String> selectSnByEmployeeId(Long id) {
		return roleMapper.selectSnByEmployeeId(id);
	}
}
